#!/usr/bin/env python3

import os
import sys
import glob
import logging
import logging.config

from {{ cookiecutter.application_name }} import main


def init_logging():
    # setup logging throughout engine
    # setup logging - output to log tab, status message, dialogs
    config = {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "simple": {
                "format": "%(asctime)s - (%(funcName)s):%(name)s:%(levelname)s - %(message)s",
                "datefmt": "%m/%d/%Y %H:%M:%S"
            }
        },

        "handlers": {
            "console": {
                "class": "logging.StreamHandler",
                "level": "DEBUG",
                "formatter": "simple",
                "stream": "ext://sys.stdout"
            },

            "info_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "simple",
                "filename": "logs/info.log",
                "maxBytes": 10485760,
                "backupCount": 20,
                "encoding": "utf8"
            },

            "error_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "ERROR",
                "formatter": "simple",
                "filename": "logs/errors.log",
                "maxBytes": 10485760,
                "backupCount": 20,
                "encoding": "utf8"
            },

            "debug_file_handler": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "DEBUG",
                "formatter": "simple",
                "filename": "logs/debug.log",
                "maxBytes": 10485760,
                "backupCount": 20,
                "encoding": "utf8"
            }
        },

        "loggers": {
            "my_module": {
                "level": "ERROR",
                "handlers": ["console"],
                "propagate": "no"
            }
        },

        "root": {
            "level": "DEBUG",
            "handlers": ["console", "info_file_handler", "error_file_handler", "debug_file_handler"]
        }
    }
    if not os.path.exists('logs'):
        os.mkdir('logs')
    logging.config.dictConfig(config)
    _logger = logging.getLogger(__name__)
    return _logger


if __name__ == "__main__":

    # remove old log files
    for f in glob.glob(os.path.join(os.path.abspath(os.path.curdir), '*.log')):
        os.remove(f)

    _logger = init_logging()
    logging.debug('Starting {} GUI'.format('MainWindow'))

    main(sys.argv)
